<?php

/**
 * ContactForm class.
 * ContactForm is the data structure for keeping
 * contact form data. It is used by the 'contact' action of 'SiteController'.
 */
class CalcForm extends CFormModel
{
	private $_product;
	private $_formatWidth;
	private $_formatHeight;
	
	public $colority;
	public $edition;
	public $paperKind;
	public $density;
	public $baseColority;
	public $format;
	
	public function getFormatWidth()
	{
		if (!empty($this->_formatWidth))
			return $this->_formatWidth;
		else
			return $this->formatModel->ut_format_width;
	}
	public function getFormatHeight()
	{
		if (!empty($this->_formatHeight))
			return $this->_formatHeight;
		else
			return $this->formatModel->ut_format_height;
	}
	public function setFormatWidth($value)
	{
		$this->_formatWidth=$value;
	}
	public function setFormatHeight($value)
	{
		$this->_formatHeight=$value;
	}
	
	public function getColorityModel()
	{
		return CActiveRecord::model('Colority')->findByPk($this->colority);
	}

	public function getPaperKindModel()
	{
		return CActiveRecord::model('PaperKind')->findByPk($this->paperKind);
	}	

	public function getPaperModel()
	{
		return CActiveRecord::model('Paper')->findByAttributes(array('ut_paper_kind_id'=>$this->paperKind,'ut_density_id'=>$this->density));
	}	
	
	public function getDensityModel()
	{
		return CActiveRecord::model('Density')->findByPk($this->density);
	}		
	
	public function getFormatModel()
	{
		return CActiveRecord::model('Format')->findByPk($this->format);
	}		
	
	public function getProduct()
	{
		if ($this->_product)
			return $this->_product;
		else
			return 1;
	}
	
	public function setProduct($value)
	{
		$this->_product = $value;
	}
	
	/**
	 * Declares the validation rules.
	 */
	public function rules()
	{
		return array(
			// required
			array('colority, edition, density, paperKind, formatWidth,formatHeight', 'required'),
			array('edition, formatWidth,formatHeight', 'type', 'type'=>'integer'),
			array('baseColority, format', 'safe'),
		);
	}

	/**
	 * Returns the list of attribute names.
	 * By default, this method returns all public properties of the class.
	 * You may override this method to change the default.
	 * @return array list of attribute names. Defaults to all public properties of the class.
	 */
	public function attributeNames()
	{
		$result=parent::attributeNames();
		$result[]='product';
		$result[]='formatWidth';
		$result[]='formatHeight';
		return $result;
	}
	
	/**
	 * Declares customized attribute labels.
	 * If not declared here, an attribute would have a label that is
	 * the same as its name with the first letter in upper case.
	 */
	public function attributeLabels()
	{
		return array(
	//		'press'=>'печатная машина',
			'colority'=>'цветность',
			'format'=>'формат',
			'edition'=>'тираж',
			'paperKind'=>'бумага',
			'density'=>'плотность',
			'baseColority'=>'без черного',
		);
	}
	
	public function calculate($recalc=true)
	{		
		$press = Press::model('Press')->findCanPrint($this);

		if($recalc)
		{
			CActiveRecord::model('UtCalc')->deleteAllByAttributes(array('id'=>Yii::app()->session->sessionId));			
			foreach ($press as $pressModel){
				$pressModel->calcPress($this);
			}
		}
 		$dataProvider=new CArrayDataProvider($press, array(
				'keyField'=>'ut_press_id',
				'pagination'=>false,
		));		
		return $dataProvider;
	}
	
}